var munbolClass = {
	win: null,
	layout: null,
	form: null,
	grid: null,
    codeMunicipio: null,
    codeBoleta: null,
	actionBar: null,	
	widgetWin: function(){
        var me = this;
		var windows = new dhtmlXWindows();
		me.win = windows.createWindow('window_1', 0, 0, 500, 510);
		me.win.setText('Boletas Asignadas al Usuario');
		me.win.clearIcon();
		me.win.button('park').hide();
		me.win.button('minmax1').hide();
		me.win.denyResize();
		me.win.setModal(1);
		me.win.centerOnScreen();
        widgetBar(me.win,4);
        me.layout = me.win.attachLayout('2E');
	},
    widgetForm: function(){
        var me = this,
            cell,
            str;
        cell = me.layout.cells('a');
        cell.setHeight(185);
        cell.hideHeader();
        str = [{
            type: "label",
            label: "DATOS DE AVANCE DEL MUNICIPIO",
            className:"fontBold",
            offsetLeft: 20,
            offsetTop: 10
        },{
            type: "label",
            list: [{
                type: "label",
                label: "Departamento:",
                labelHeight:1,
                className:"fontBold"
            }, {
                type: "newcolumn"
            }, {
                type: "label",
                name: "lab_dep",
                labelHeight:1,
                label: ""
            }]
        },{
            type: "label",
            list: [{
                type: "label",
                label: "Codigo:",
                className:"fontBold"
            }, {
                type: "newcolumn"
            }, {
                type: "label",
                name: "lab_code",
                label: ""
            }, {
                type: "newcolumn"
            }, {
                type: "label",
                label: "Detalle del Municipio:",
                className:"fontBold"
            }, {
                type: "newcolumn"
            }, {
                type: "label",
                name: "lab_muni",
                label: ""
            }]
        }, {
            type: "label",
            list: [{
                type: "label",
                label: "(%)Porcentaje de Avance Datos Transcritos:",
                labelHeight:1,
                className:"fontBold"
            }, {
                type: "newcolumn"
            }, {
                type: "label",
                name: "lab_tras",
                labelHeight:1,
                label: ""
            }]
        }, {
            type: "label",
            list: [{
                type: "label",
                label: "(%)Porcentaje de Avance Datos NO Transcritos:",
                labelHeight:1,
                className:"fontBold"
            }, {
                type: "newcolumn"
            }, {
                type: "label",
                name: "lab_ntra",
                labelHeight:1,
                label: ""
            }]
        }];
        me.form = cell.attachForm(str);
    },
	widgetGrid: function(){
		var me = this;	
		var panel = me.layout.cells('b');
		panel.hideHeader();
		panel.fixSize(true, true);
        me.statusGrid = panel.attachStatusBar();

		me.grid = panel.attachGrid();
		//me.grid.setHeader("asig_bole,asig_mun,mun_detalle,Boleta,Fecha Inicio,Fecha Final,Observacion,usu_id,Nombre");
        me.grid.setHeader("id,Code Boleta,Detail Boleta,User,Departamento,Codigo Municipio,Municipio");
		me.grid.setColTypes("ro,ro,ro,ro,ro,ro,ro");
		me.grid.setColAlign("right,center,center,left,left,center,left");
		me.grid.setInitWidths("30,*,*,*,150,100,*");
		me.grid.init();
        me.grid.setColumnColor("#E5E5E5");
		me.grid.load(mainPath+"index.php/Asig_usu/asigUMBgrid_data?usu_id="+idUsuario, function(){
            me.grid.groupBy(2);
            me.grid.setColumnHidden(1,true);
            me.grid.setColumnHidden(2,true);
            me.grid.setColumnHidden(3,true);

			//me.statusGrid.setText("Numero de Registros: "+me.grid.getRowsNum());
		},"json");
        me.grid.attachEvent('onRowDblClicked', function(rId, cInd){
            var cbol = me.grid.cells(rId,1).getValue();
            var depa = me.grid.cells(rId,4).getValue();
            var muni = me.grid.cells(rId,5).getValue();
            var code = me.grid.cells(rId,6).getValue();

            me.codeMunicipio = me.grid.cells(rId,5).getValue();
            me.codeBoleta = me.grid.cells(rId,1).getValue();

            me.grid.destructor();
            me.grid = panel.attachGrid();
            me.grid.setImagePath(mainPath+'dhtmlx/imgs/');
            me.grid.setHeader("id,Modulos de la Boleta,Completado,%");
            me.grid.setColTypes("ro,ro,img,ro");
            me.grid.setColAlign("left,left,center,left");
            me.grid.setInitWidths("30,*,80,*");
            me.grid.init();
            me.grid.setColumnColor("#E5E5E5");
            var cont = 0;
            me.grid.load(mainPath+"index.php/Asig_usu/loadModuleAsig?usu_id="+idUsuario+"&muni="+muni+"&bole="+cbol, function(){
                if(me.grid.getRowsNum() > 0){
                    me.grid.setColumnHidden(3,true);
                    var no = me.grid.cells(me.grid.getRowsNum(),3).getValue();
                    var si = 100-parseInt(no);
                    me.form.setItemLabel("lab_tras", si+'%');
                    me.form.setItemLabel("lab_ntra", no+'%');

                    actionBar.enableItem('btn_saveas');
                    actionBar.enableItem('btn_edit');
                }
                else
                    dhtmlx.alert({
                        text:"Error, La transcripción de esta boleta todavia no esta habilitada.",
                        callback:  function(status){
                            if(status == true){
                                me.widgetForm();
                                me.widgetGrid();
                            }
                        }
                    });
            },"json");

            me.form.setItemLabel("lab_dep", depa);
            me.form.setItemLabel("lab_muni", code);
            me.form.setItemLabel("lab_code", muni);
            /*
            dhtmlxAjax.get(mainPath+"index.php/Asig_usu/loadModuleAsig?usu_id="+idUsuario+"&muni="+muni+"&bole="+cbol, function(loader){
                console.log(loader.xmlDoc.response);
            });
            */
        });
	},

	widgetMain: function(){
        var me = munbolClass;
        var user = idUsuario;
		me.widgetWin();
        me.widgetForm();
		me.widgetGrid();
        actionBar.attachEvent("onClick", function(itemId){
            if(me.grid.getSelectedRowId()){
                var rowId = me.grid.cells(me.grid.getSelectedRowId(),0).getValue();
                (me.codeBoleta == 'BINF')?boletaInfraClass.formCont = parseInt(rowId)-1:boletaGestionClass.formCont = parseInt(rowId)-1;
            }
            else
                (me.codeBoleta == 'BINF')?boletaInfraClass.formCont = 0:boletaGestionClass.formCont = 0;
            switch(itemId){
                case 'btn_saveas':
                    dhxLayout.cells("b").collapse();
                    if(me.codeBoleta == 'BINF')
                        boletaInfraClass.widgetMain('btn_edit');
                    else
                        boletaGestionClass.widgetMain('btn_edit');
                    me.win.close();
                break;
                case 'btn_edit':
                    dhxLayout.cells("b").collapse();
                    if(me.codeBoleta == 'BINF')
                        boletaInfraClass.widgetMain('btn_save');
                    else
                        boletaGestionClass.widgetMain('btn_save');

                    me.win.close();
                break;
                case 'btn_clear':
                    me.widgetForm();
                    me.widgetGrid();
                break;
            }
        });
    }
 };